%% pco_camera_info
%  [errorCode,glvar] = pco_camera_setup(glvar)
% 
% * Input parameters :
% 
%  struct     glvar
%
% * Output parameters :
%
%  errorCode
% struct     glvar
% 
%  does retrive some information of the connected camera
% especially ty of interface
%
%  structure glvar is used to set different modes for
% load/unload library
% open/close camera SDK
%
%  glvar.do_libunload: 1 unload lib at end
% glvar.do_close:     1 close camera SDK at end
% glvar.camera_open:  open status of camera SDK
% glvar.out_ptr:      libpointer to camera SDK handle
%
%  function workflow
% camera information is readout
% errorCode and if available glvar is returned
%
function [errorCode,glvar] = pco_camera_info(glvar)

if(~exist('texton','var'))
    texton = uint8(0);
end


if((exist('glvar','var'))&& ...
        (isfield(glvar,'do_libunload'))&& ...
        (isfield(glvar,'do_close'))&& ...
        (isfield(glvar,'camera_open'))&& ...
        (isfield(glvar,'out_ptr')))
    unload=glvar.do_libunload;
    cam_open=glvar.camera_open;
    do_close=glvar.do_close;
    out_ptr=glvar.out_ptr;
else
    error('parameter cant be none.');
end

%test camera recording state and stop camera, if camera is recording
act_recstate = uint16(0);
[errorCode,out_ptr,act_recstate] = calllib('PCO_CAM_SDK', 'PCO_GetRecordingState', out_ptr,act_recstate);
if(errorCode)
    disp(['PCO_GetRecordingState failed with error ',num2str(errorCode,'%X')]);
end

ml_cam_type.wSize=uint16(1364);
cam_type=libstruct('PCO_CameraType',ml_cam_type);
[errorCode,out_ptr,cam_type] = calllib('PCO_CAM_SDK', 'PCO_GetCameraType', out_ptr,cam_type);
if(errorCode)
    disp(['PCO_GetCameraType failed with error ',num2str(errorCode,'%08X')]);
end
interface=uint16(cam_type.wInterfaceType);

disp(['interface is: ',int2str(interface)]);
disp(['record state is: ',int2str(act_recstate)]);

disp(' ');
disp('Camera Hardware versions:');
z=1;
for n=1:10
    a=strfind(cam_type.strHardwareVersion(z:z+15),0);
    if(a+((n-1)*62)>z)
        b=char(cam_type.strHardwareVersion(z:z+a-1));
        batch=uint16(cam_type.strHardwareVersion(z+17:z+17))*256;
        batch=batch+uint16(cam_type.strHardwareVersion(z+16:z+16));
        rev=uint16(cam_type.strHardwareVersion(z+19:z+19))*256;
        rev=rev+uint16(cam_type.strHardwareVersion(z+18:z+18));
        var=uint16(cam_type.strHardwareVersion(z+21:z+21))*256;
        var=var+uint16(cam_type.strHardwareVersion(z+20:z+20));
        disp([b,blanks(16-a),':',int2str(batch),'.',int2str(rev),'.',int2str(var)]);
    end
    z=z+62;
end
disp(' ');
disp('Camera Firmware versions:');
z=7;
for n=1:10
    a=strfind(cam_type.strFirmwareVersion(z:z+15),0);
    a=a(1);
    if(a+((n-1)*64)>z)
        b=char(cam_type.strFirmwareVersion(z:z+a-1));
        minor=uint8(cam_type.strFirmwareVersion(z+16:z+16));
        major=uint8(cam_type.strFirmwareVersion(z+17:z+17));
        variant=uint16(cam_type.strFirmwareVersion(z+19:z+19))*256;
        variant=variant+uint16(cam_type.strFirmwareVersion(z+18:z+18));
        disp([b,blanks(16-a),':',int2str(variant),'  ',int2str(major),'.',int2str(minor)]);
    end
    z=z+64;
end

end